import java.io.*;
import java.net.URL;


/**
 * Reads the Android files from a URL, writes it a file object and removes all the comments from it
 *
 */
public class URLFileReader {
	
	public URLFileReader () {
		
	}
	
	/**
	 * Reads an Android file from a URL and writes it to a file object. Then, removes all comments from it
	 * @param urlPath The path containing the Android code
	 * @param classAndRevisionName The class and revision number
	 * @return A file containing the Java code with all comments removed
	 */
	public File readFromURL(String urlPath, String classAndRevisionName) {
		int ch;

		boolean slashCommentFound = false;
		boolean starCommentFound = false;
		boolean firstSlashFound = false;
		boolean firstStarFound = false;
		boolean closingStarFound = false;
		boolean startDoubleQuoteFound = false;
		int lastChar;
		StringBuilder strBuilder = new StringBuilder();
		File outputFile = new File(classAndRevisionName);
		
		try {
			URL url = new URL(urlPath);
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));

			while ((ch = reader.read()) != -1) {

				lastChar = ch;

				if (ch == '\"') {
					if (startDoubleQuoteFound == false) {
						startDoubleQuoteFound = true;
					} else if (startDoubleQuoteFound == true) {
						startDoubleQuoteFound = false;
					}
				}

				if (startDoubleQuoteFound
						&& (starCommentFound == true || slashCommentFound == true)) {
					continue;
				}
				if (ch == '/') {
					if (starCommentFound == true && closingStarFound == false) {
						continue;
					}
					if (closingStarFound && starCommentFound == true) {
						starCommentFound = false;
						closingStarFound = false;
						firstStarFound = false;
						continue;
					} else if (firstSlashFound && slashCommentFound == false
							&& starCommentFound == false) {
						slashCommentFound = true;
						firstSlashFound = false;
						continue;
					} else if (slashCommentFound == false
							&& starCommentFound == false
							&& startDoubleQuoteFound == false) {
						firstSlashFound = true;
						continue;
					}
				}
				if (ch == '*') {
					if (starCommentFound) {
						closingStarFound = true;
						continue;
					}
					if (firstSlashFound && starCommentFound == false) {
						starCommentFound = true;
						firstSlashFound = false;
						continue;
					} else if (firstStarFound == false
							&& starCommentFound == true) {
						firstStarFound = true;
						continue;
					}
				}
				if (ch == '\n') {
					if (slashCommentFound) {
						slashCommentFound = false;
						firstStarFound = false;
						firstSlashFound = false;
						starCommentFound = false;
						System.out.print((char) ch);
						continue;
					}
				}

				if (starCommentFound == true && closingStarFound == false) {
					continue;
				}

				if (ch != '/' && ch != '*') {
					if (closingStarFound) {
						strBuilder.append((char) lastChar);
					}

					closingStarFound = false;
					firstSlashFound = false;
					firstStarFound = false;
					closingStarFound = false;

				}

				if (slashCommentFound == false && starCommentFound == false) {
					strBuilder.append((char) ch);
				}
			}

			BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));
			writer.write(strBuilder.toString());
			
			reader.close();
			writer.close();

		} catch (FileNotFoundException ex) {
			System.out.println(classAndRevisionName + " not found");
			ex.printStackTrace();
		} catch (Exception ex) {
			System.out.println("Error reading file " + classAndRevisionName);
			ex.printStackTrace();
		}
		
		
		return outputFile;
	}
}
